package funciones;

public class Basica extends Funcion{
	/**
	 * Funciones Basicas: +, -, * y /
	 * @param arg1 Argumento1
	 * @param oper Operador (|+|-|*|/|)
	 * @param arg2 Argumento2
	 */
	public Basica(String arg1, int oper, String arg2) {
		this.oper = oper;
		arg1= arg1.length()>0?arg1:"0";
		arg2= arg2.length()>0?arg2:"0";
		this.arg1 = this.minimaExpresion(validar(arg1));
		this.arg2 = this.minimaExpresion(validar(arg2));
	}
	/**
	 * Fija los argumentos y el operador para la evaluacion
	 * @param arg1 Argumento1
	 * @param oper Operador
	 * @param arg2 Argumento2
	 */
	public void set(String arg1, int oper, String arg2) {
		this.oper = oper;
		arg1= arg1.length()>0?arg1:"0";
		arg2= arg2.length()>0?arg2:"0";
		this.arg1 = this.minimaExpresion(validar(arg1));
		this.arg2 = this.minimaExpresion(validar(arg2));
	}
					
	
	public String evaluarSimbolicamente() {
		
		switch (this.oper) {
		case '=':
			eval[0]=String.valueOf((Double.valueOf(numerador)/Double.valueOf(denominador)) );
			eval[1]="1";
			break;
		case '+':
			eval[0] = String.valueOf((Double.valueOf(arg1[0])*Double.valueOf(arg2[1])) + (Double.valueOf(arg1[1])*Double.valueOf(arg2[0])) );
			eval[1] = String.valueOf( (Double.valueOf(arg1[1])*Double.valueOf(arg2[1])) );			
			break;
		case '-':
			eval[0] = String.valueOf((Double.valueOf(arg1[0])*Double.valueOf(arg2[1])) - (Double.valueOf(arg1[1])*Double.valueOf(arg2[0])) );
			eval[1] = String.valueOf( (Double.valueOf(arg1[1])*Double.valueOf(arg2[1])) );			
			break;
		case '*':
			eval[0]=String.valueOf((Double.valueOf(arg1[0])*(Double.valueOf(arg2[0]))) );
			eval[1]=String.valueOf((Double.valueOf(arg1[1])*(Double.valueOf(arg2[1]))) );
			break;
		case '/':
			eval[0]=String.valueOf((Double.valueOf(arg1[0])*(Double.valueOf(arg2[1]))) );
			eval[1]=String.valueOf((Double.valueOf(arg1[1])*(Double.valueOf(arg2[0]))) );
			break;
		}				
		if( Double.valueOf(eval[1])==0) {
			eval[1]=eval[0]="0";
			return "INF";
		}
		if( (Double.valueOf(eval[0])==0)&&(Double.valueOf(eval[1])!=0)) {
			eval[1]=eval[0]="0";
			return eval[0];
		}else {
			eval = minimaExpresion(eval);
			numerador = eval[0];
			denominador = eval[1];
			return Double.valueOf(denominador)==1?numerador:numerador+"/"+denominador;	
		}				
	}
	
	public Double evaluar() {
		//evaluarSimbolicamente();
		double x, y;
		x = new Double(numerador);
		y = new Double(denominador);

		num = x/y;
		return num;
	}
}
